﻿using System;
using System.Collections.Generic;
using System.Text;
using Portal.Interface;
using Portal.Entity;
using System.Data;
using MyLib;
using System.Web;

namespace Workflow.Logic
{
    public class DBOrgLogic:IOrg
    {
        public virtual bool CheckUser(string loginName, string password)
        {
            return true;
        }

        static List<string> _domains = new List<string>();
        public virtual List<string> GetDomains()
        {
            if (_domains.Count > 0)
            {
                return _domains;
            }
            _domains.Add("ULTIMUS");
            return _domains; ;
        }

        public virtual List<ResourceEntity> GetOrgList()
        {
            List<ResourceEntity> list = new List<ResourceEntity>();
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM V_DEPARTMENT");
            foreach (DataRow row in dt.Rows)
            {
                ResourceEntity ety = new ResourceEntity();
                ety.CNNAME = ConvertUtil.ToString(row["DEPARTMENTNAME"]);
                ety.ENNAME = ConvertUtil.ToString(row["DEPARTMENTNAME"]);
                ety.RESOURCEID = ConvertUtil.ToInt32(row["DEPARTMENTID"]);
                ety.PARENTID = ConvertUtil.ToInt32(row["PARENTID"]);
                ety.TYPE = "Org";
                list.Add(ety);
            }

            return list;
        }

        public virtual List<ResourceEntity> FindOrg(string orgName)
        {
            List<ResourceEntity> list = GetOrgList();
            return list.FindAll(p => p.CNNAME.IndexOf(orgName) >= 0);
        }

        public virtual List<UserEntity> GetUserList(string orgId)
        {
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM V_EMPLOYEE WHERE LEN(SHORTNAME)>0 AND DEPARTMENTID=" + orgId);
            List<UserEntity> list = new List<UserEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                UserEntity user = new UserEntity();
                user.CNNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.ENNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.LOGINNAME = ConvertUtil.ToString(dr["SHORTNAME"]);
                user.JOBFUNCTION = ConvertUtil.ToString(dr["TITLE"]);
                user.USERID = ConvertUtil.ToInt32(dr["EMPLOYEEID"]);
                list.Add(user);
            }

            return list;
        }

        public virtual UserEntity GetUserEntity(string loginName)
        {
            if (HttpContext.Current.Session["UserEntity"] != null)
            {
                return HttpContext.Current.Session["UserEntity"] as Portal.Entity.UserEntity;
            }
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM V_EMPLOYEE WHERE SHORTNAME = '" + loginName.Replace("\\", "/") + "' ");
            UserEntity user = new UserEntity();
            foreach (DataRow dr in dt.Rows)
            {
                user.CNNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.ENNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.LOGINNAME = ConvertUtil.ToString(dr["SHORTNAME"]);
                user.JOBFUNCTION = ConvertUtil.ToString(dr["TITLE"]);
                user.USERID = ConvertUtil.ToInt32(dr["EMPLOYEEID"]);
                user.DIRECTREPORTID = ConvertUtil.ToInt32(dr["SUPERVISORID"]);
                user.EXT02 = ConvertUtil.ToString(dr["DEPARTMENTID"]);
            }

            HttpContext.Current.Session["UserEntity"] = user;
            return user;
        }

        UserEntity getUserEntity(string loginName)
        {
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM V_EMPLOYEE WHERE SHORTNAME = '" + loginName.Replace("\\", "/") + "' ");
            UserEntity user = new UserEntity();
            foreach (DataRow dr in dt.Rows)
            {
                user.CNNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.ENNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.LOGINNAME = ConvertUtil.ToString(dr["SHORTNAME"]);
                user.JOBFUNCTION = ConvertUtil.ToString(dr["TITLE"]);
                user.USERID = ConvertUtil.ToInt32(dr["EMPLOYEEID"]);
                user.DIRECTREPORTID = ConvertUtil.ToInt32(dr["SUPERVISORID"]);
                user.EXT02 = ConvertUtil.ToString(dr["DEPARTMENTID"]);
            }
            return user;
        }

        public virtual List<UserEntity> GetUserList(string orgId, string jobFunction)
        {
            List<UserEntity> list = GetUserList(orgId);
            return list.FindAll(p => p.JOBFUNCTION.Trim().ToUpper() == jobFunction.ToUpper().Trim());
        }

        public virtual List<UserEntity> FindUser(string loginName, string name)
        {
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM V_EMPLOYEE WHERE SHORTNAME like '%" + loginName.Replace("\\", "/") + "%' or FIRSTNAME like '%" + name + "%'");
            List<UserEntity> list = new List<UserEntity>();
            foreach (DataRow dr in dt.Rows)
            {
                UserEntity user = new UserEntity();
                user.CNNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.ENNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.LOGINNAME = ConvertUtil.ToString(dr["SHORTNAME"]);
                user.JOBFUNCTION = ConvertUtil.ToString(dr["TITLE"]);
                user.USERID = ConvertUtil.ToInt32(dr["EMPLOYEEID"]);
                list.Add(user);
            }

            return list;
        }


        public List<ResourceEntity> GetUserGroups(string loginName)
        {
            List<ResourceEntity> groups = new List<ResourceEntity>();
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM  V_GROUPSMEMBER ORDER BY GROUPID,GROUPMEMBERTYPE");
            List<ResourceEntity> depts = GetUserDepartments(loginName);
            foreach (DataRow dr in dt.Rows)
            {
                string type = ConvertUtil.ToString(dr["GROUPMEMBERTYPE"]);
                int GROUPMEMBERID = ConvertUtil.ToInt32(dr["GROUPMEMBERID"]);
                int GROUPID = ConvertUtil.ToInt32(dr["GROUPID"]);
                ResourceEntity group=new ResourceEntity();
                group.RESOURCEID=GROUPID;
                UserEntity user = getUserEntity(loginName);
                switch (type)
                {
                    //1:组成员为用户
                    case "1":
                        if (user.USERID == GROUPMEMBERID && !groups.Exists(p=>p.RESOURCEID==GROUPID))
                        {
                            groups.Add(group);
                        }
                        break;
                    //2:组成员为当前部门
                    case "2":
                        if (user.EXT02 == GROUPMEMBERID.ToString() && !groups.Exists(p => p.RESOURCEID == GROUPID))
                        {
                            groups.Add(group);
                        }
                        break;
                    //3:组成员为部门及下属所有子部门
                    case "3":
                        foreach (ResourceEntity dept in depts)
                        {
                            if (dept.RESOURCEID == GROUPMEMBERID && !groups.Exists(p => p.RESOURCEID == GROUPID))
                            {
                                groups.Add(group);
                            }
                        }
                        break;
                    //3:排除该人
                    case "99":
                        if (user.USERID == GROUPMEMBERID && groups.Exists(p => p.RESOURCEID == GROUPID))
                        {
                            groups.Remove(groups.Find(p => p.RESOURCEID == GROUPID));
                        }
                        break;
                }
            }
            return groups;
        }

        public List<ResourceEntity> GetUserDepartments(string loginName)
        {
            object obj= DataAccess.Instance("BizDB").ExecuteScalar("SELECT DEPARTMENTID FROM V_EMPLOYEE WHERE SHORTNAME='" + loginName.Replace("\\","/")+ "'");
            List<ResourceEntity> depts = new List<ResourceEntity>();
            string deptId = ConvertUtil.ToString(obj);
            if (string.IsNullOrEmpty(deptId))
            {
                deptId = "-1";
            }
            while (true)
            {
                DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM V_DEPARTMENT WHERE DEPARTMENTID=" + deptId);
                if (dt.Rows.Count > 0)
                {
                    ResourceEntity resource = new ResourceEntity();
                    resource.RESOURCEID = ConvertUtil.ToInt32(dt.Rows[0]["DEPARTMENTID"]); //部门ID
                    resource.PARENTID = ConvertUtil.ToInt32(dt.Rows[0]["PARENTID"]); //PARENTID
                    resource.CNNAME = ConvertUtil.ToString(dt.Rows[0]["DEPARTMENTNAME"]); //DEPARTMENTNAME
                    resource.ENNAME = ConvertUtil.ToString(dt.Rows[0]["DEPARTMENTNAME"]); //DEPARTMENTNAME
                    depts.Add(resource);

                    deptId = ConvertUtil.ToString(resource.PARENTID);
                }
                else
                {
                    break;
                }

            }
            return depts;
        }


        public UserEntity GetUserEntityByID(string userID)
        {
            DataTable dt = DataAccess.Instance("BizDB").ExecuteDataTable("SELECT * FROM V_EMPLOYEE WHERE EMPLOYEEID = " + userID + " ");
            UserEntity user = new UserEntity();
            foreach (DataRow dr in dt.Rows)
            {
                user.CNNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.ENNAME = ConvertUtil.ToString(dr["FIRSTNAME"]);
                user.LOGINNAME = ConvertUtil.ToString(dr["SHORTNAME"]);
                user.JOBFUNCTION = ConvertUtil.ToString(dr["TITLE"]);
                user.USERID = ConvertUtil.ToInt32(dr["EMPLOYEEID"]);
                user.DIRECTREPORTID = ConvertUtil.ToInt32(dr["SUPERVISORID"]);
                user.EXT02 = ConvertUtil.ToString(dr["DEPARTMENTID"]);
            }

            return user;
        }
    }
}
